package xing.appwidget.label

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import xing.appwidget.R
import xing.appwidget.databinding.LayoutLabelManagerBinding
import xing.appwidget.storage.LabelStorageHelper

class LabelManagerFragment(private val editMode: Boolean) : DialogFragment() {

    companion object {
        fun start(activity: AppCompatActivity, editMode: Boolean,
                  onLabelSelectedListener: OnLabelSelectedListener? = null,
                  selectedLabels: Collection<String>? = null) {
            val fm = activity.supportFragmentManager
            val fragment = LabelManagerFragment(editMode)
            fragment.onSelectDoneListener = onLabelSelectedListener
            fragment.selectedLabels = selectedLabels
            fragment.show(fm, LabelManagerFragment::class.java.simpleName)
        }
    }

    private var onSelectDoneListener: OnLabelSelectedListener? = null
    private var selectedLabels: Collection<String>? = null

    private lateinit var binding: LayoutLabelManagerBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.Dialog_FullScreen)
        LabelStorageHelper.init(context!!)
        initObserver()
    }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        binding = LayoutLabelManagerBinding.inflate(inflater, container, false)
        binding.root.setOnClickListener { dismiss() }
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        with(binding) {
            rvLabels.setEditMode(editMode)
            val selectedLabels = selectedLabels
            if (selectedLabels != null) {
                rvLabels.setSelected(selectedLabels)
            }
            groupEditBtns.visibility = if (editMode) View.VISIBLE else View.GONE
            ivDone.visibility = if (!editMode) View.VISIBLE else View.GONE

            ivDelete.setOnClickListener {
                if (ivDelete.isSelected) {
                    for (label in rvLabels.getSelectedLabels()) {
                        LabelStorageHelper.delLabel(label)
                    }
                }
                ivDelete.isSelected = !ivDelete.isSelected
            }
            ivDone.setOnClickListener {
                if (ivDone.isEnabled) {
                    onSelectDoneListener?.onSelected(rvLabels.getSelectedLabels())
                    dismiss()
                }
            }
            ivAdd.setOnClickListener {
                LabelDetailFragment.start(activity as AppCompatActivity, null, true)
            }
        }
    }

    private fun initObserver() {
        LabelStorageHelper.labelSetLd.observe(this, Observer {
            if (it != null) binding.rvLabels.setData(it)
        })
    }

    override fun onResume() {
        super.onResume()
    }

    override fun dismiss() {
        super.dismissAllowingStateLoss()
    }

    interface OnLabelSelectedListener {
        fun onSelected(labels: Collection<String>)
    }
}
